package br.com.registrofinancas.controller;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.List;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import br.com.registrofinancas.R;
import br.com.registrofinancas.adapters.FinancasGastosAdapter;
import br.com.registrofinancas.database.MovimentosEntradaDAO;
import br.com.registrofinancas.database.MovimentosSaidaDAO;
import br.com.registrofinancas.model.ExpenseVO;
import br.com.registrofinancas.model.IncomeVO;
import br.com.registrofinancas.util.CalendarUtilNavegavacaoMes;
import br.com.registrofinancas.view.FinancasActivity;

public class FinancasActivityGastosController {
	
	private static FinancasActivityGastosController instance = new FinancasActivityGastosController();
	private FinancasActivity financasActivity;
	private GregorianCalendar mes;
	private TextView mesAnoHeaderNavegacao;
	private SimpleDateFormat formatoMesAnoHeaderNavegacao;
	private ListView listVIew;
	private MovimentosSaidaDAO dao;
	private FinancasActivityGastosController() {}
	
	public static FinancasActivityGastosController getInstance() {
		return instance;
	}
	
	private OnClickListener btnPreviousMonth(){
		return new OnClickListener() {			
			@Override
			public void onClick(View v) {
				GregorianCalendar mMes = CalendarUtilNavegavacaoMes.setPreviousMonth(mes);
				CalendarUtilNavegavacaoMes.setMes(financasActivity, mMes, mesAnoHeaderNavegacao, formatoMesAnoHeaderNavegacao);
				getSaidas();
			}
		};
	}
	
	private OnClickListener btnNextMonth(){
		return new OnClickListener() {			
			@Override
			public void onClick(View v) {
				GregorianCalendar mMes = CalendarUtilNavegavacaoMes.setNextMonth(mes);
				CalendarUtilNavegavacaoMes.setMes(financasActivity, mMes, mesAnoHeaderNavegacao, formatoMesAnoHeaderNavegacao);
				getSaidas();
			}
		};
	}

	public void setInitDadosTab(FinancasActivity financasActivity, ListView listVIew) {
		this.financasActivity = financasActivity;
		this.listVIew = listVIew;		
			
		setupHeaderData();
		
		CalendarUtilNavegavacaoMes.setMes(financasActivity, mes, mesAnoHeaderNavegacao, formatoMesAnoHeaderNavegacao);
		
		getSaidas();
		
		setupListeners();
				
	}
		
	private void getSaidas(){		
		new BuscaSaidas().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}
	
	private class BuscaSaidas extends AsyncTask<Void, Void, List<ExpenseVO>>{

		MovimentosSaidaDAO dao = new MovimentosSaidaDAO(financasActivity);
		ProgressDialog progressDialog;
		double valorTotal = 0;						
		
		@Override
		protected void onPreExecute() {
			progressDialog = ProgressDialog.show(financasActivity, "Aguarde", "Buscando dados ...");
		}
		
		
		@Override
		protected List<ExpenseVO> doInBackground(Void... params) {
			List<ExpenseVO> saidas = dao.getSaidasPorMes(mes.getTime());
			valorTotal = dao.getValorTotalSaidasPorMes(mes.getTime());			
			return saidas;
		}
		
		protected void onPostExecute(List<ExpenseVO> saidas) {
			FinancasGastosAdapter adapter = new FinancasGastosAdapter(financasActivity, saidas);
			listVIew.setAdapter(adapter);
			setValorTotal(valorTotal);
			progressDialog.dismiss();
		};
		
	}
	
	private void setupListeners(){
		((ImageButton) financasActivity.findViewById(R.id.financas_navegacao_data_gastos_ib_previous_month)).setOnClickListener(btnPreviousMonth());
		((ImageButton) financasActivity.findViewById(R.id.financas_navegacao_data_gastos_ib_next_month)).setOnClickListener(btnNextMonth());
	}
	
	private void setupHeaderData(){
		mes = (GregorianCalendar) GregorianCalendar.getInstance();
		mesAnoHeaderNavegacao = (TextView) financasActivity.findViewById(R.id.financas_navegacao_data_gastos_tv_mes_ano_atual);
		formatoMesAnoHeaderNavegacao = new SimpleDateFormat("MMM/yyyy");
	}
		
	private void setValorTotal(Double valorTotal){
		TextView tvValorTotal = (TextView) financasActivity.findViewById(R.id.financas_lista_gastos_tv_valor_total);					
		tvValorTotal.setText(String.valueOf(valorTotal));	
	}
	
}
